package com.bycx.wechat.util;

import com.bycx.core.startup.AppConfig;
import com.bycx.core.util.StringUtils;

import java.net.URLEncoder;

public class WeChatAuthorizeInstallUtil {
	private static final String PREFIX = "https://open.weixin.qq.com/connect/oauth2/authorize";
	/**不弹出授权页面，直接跳转，只能获取用户openid*/
	private static final String SNSAPI_BASE = "snsapi_base";
	/**弹出授权页面，可通过openid拿到昵称、性别、所在地。并且，即使在未关注的情况下，只要用户授权，也能获取其信息*/
	private static final String SNSAPI_USERINFO = "snsapi_userinfo";
	/**重定向后会带上state参数，开发者可以填写a-zA-Z0-9的参数值，最多128字节*/
	private static final String STATE_VALUE = "123";	//任意满足要求的值
	private static final String APPID = "appid";
	private static final String SCOPE = "scope";
	private static final String REDIRECT_URI = "redirect_uri";
	private static final String RESPONSE_TYPE = "response_type";
	
	@SuppressWarnings("deprecation")
	public static String returnURL(String url){
		String appid = AppConfig.get("wx_appid");
		StringBuilder sb = new StringBuilder();
		if(StringUtils.isEmpty(url) && StringUtils.isEmpty(appid)){
			return null;
		}
		url = URLEncoder.encode(url);
		sb.append(PREFIX).append("?")
			.append(APPID+"="+appid).append("&")
			.append(REDIRECT_URI+"="+url).append("&")
			.append(RESPONSE_TYPE+"=code").append("&")
			.append(SCOPE+"="+SNSAPI_USERINFO).append("&")
			.append("state="+STATE_VALUE).append("#wechat_redirect");
		url = sb.toString();
		System.out.println("url = "+url);
		return url;
	}
}
